﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using System.Linq;
using DynamicListView.Controls;
namespace WPFExcelReport
{
    public class SizeColorMatrix
    {
        public string Line { get; set; }
        public string Color { get; set; }
        public string Size { get; set; }
        public int Qty { get; set; }
    }
    public class MonthlyOrderCount
    {
        public int Year { get; set; }
        public int Month { get; set; }
        public int NumberOfOrders { get; set; }
    }
    public class YCAFSPLANData : BaseFunction
    {
        public INTEXDBC INTEXDBC;
        public SETINTEXDB INTEXDB = new SETINTEXDB();
        public YCAFSPLANData()
            : base()
        {

        }
        public YCAFSPLANData(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {
            INTEXDBC = new INTEXDBC(_OracleDB, _proxy);
        }
        public YCAFSPLANData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {

        }
        public DataSet LoadAssData()
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData = INTEXDB.LIEFDAT.NewLIEFDATRow();
            dwData["LSMANDANT"] = "00";
            SResult = INTEXDBC.QueryLIEFDAT(dwData,3);
            foreach (DataRow rx in SResult.Tables[0].Rows)
            {
                rx["LSLIEFNA"] = Convert8859P1ToGB2312(rx["LSLIEFNA"].ToString());
            }
            SReturn.Merge(SResult);
            dwData = INTEXDB.FSSTAMM.NewRow();
            dwData["FSMANDANT"] = "00";
            
            SResult = INTEXDBC.QueryFSSTAMM(dwData,3);
            foreach (DataRow rx in SResult.Tables[0].Rows)
            {
                rx["FSABEZ"] = Convert8859P1ToGB2312(rx["FSABEZ"].ToString());
            }
            SReturn.Merge(SResult);
            return SReturn;
        }
        public DataSet CreateMatrix(DataTable tbABPOS)
        {
            DataSet Return = new DataSet();

            DataTable tbSC = INTEXDB.ICC_ABPOS_MAT.Clone();
            foreach (DataRow row in tbABPOS.Rows)
            {
                string art = row["ABPART"].ToString();
                string color = row["ABPFARBE"].ToString();
                string size = row["ABPGROE"].ToString().Trim();
                double qty = System.Convert.ToDouble(row["ABPSOLLMENGE7"].ToString()) * 0.01;
                
                bool added = false;
                bool newcolor = false;
                int yc = tbSC.Columns.Count;
                int y = 0;
                added = false;
                DataRow rmx = null;
                var query = from DataRow rm in tbSC.AsEnumerable()
                            where  rm["ABPART"].ToString() == art
                            && rm["ABPFARBE"].ToString() == color
                            select rm;
                foreach (DataRow rm in query)
                {
                    rmx = rm;
                }
                if (rmx == null)
                {
                    rmx = tbSC.NewRow();
                    newcolor = true;
                }
                rmx["ABPART"] = art;
                rmx["ABPFARBE"] = color;

                added = false;
                foreach (DataColumn m in tbSC.Columns)
                {
                    if (m.ColumnName == size)
                    {
                        added = true;
                        break;
                    }
                    y++;
                }
                if (!added)//未找到列；
                {
                    tbSC.Columns.Add(new DataColumn(size));
                }
                yc = tbSC.Columns.Count;
                string szQty = rmx[y].ToString();
                if (szQty.Trim() == "")
                    szQty = "0";
            
                double lastqty = System.Convert.ToDouble(szQty);
                
                
                rmx[y] = lastqty + qty;

                if (tbSC.Columns.Contains(size + "P"))
                {
                    double pqty = System.Convert.ToDouble(row["ABPSOLLMENGE8"].ToString()) * 0.01;
                    string szpQty = rmx[size + "P"].ToString();
                    if (szpQty.Trim() == "")
                        szpQty = "0";
                    double lastpqty = System.Convert.ToDouble(szpQty);
                    rmx[size + "P"] = lastpqty + pqty;
                }
               
                if (newcolor)
                {
                    tbSC.Rows.Add(rmx);
                }
            }
            double colortotal = 0;
            double total = 0;
            int ycm = tbSC.Columns.Count;
            foreach (DataRow row in tbSC.Rows)
            {
                colortotal = 0;
                int y = 0;
                string sQty = row["S"].ToString();
                if (sQty.Trim() == "") sQty = "0";
                colortotal += System.Convert.ToDouble(sQty);

                sQty = row["M"].ToString();
                if (sQty.Trim() == "") sQty = "0";
                colortotal += System.Convert.ToDouble(sQty);

                sQty = row["L"].ToString();
                if (sQty.Trim() == "") sQty = "0";
                colortotal += System.Convert.ToDouble(sQty);

                sQty = row["XL"].ToString();
                if (sQty.Trim() == "") sQty = "0";
                colortotal += System.Convert.ToDouble(sQty);

                row["TQTY"] = colortotal;
                total += colortotal;
            }
            colortotal = 0;
            total = 0;
            foreach (DataRow row in tbSC.Rows)
            {
                colortotal = 0;
                int y = 0;
                string sQty = row["SP"].ToString();
                if (sQty.Trim() == "") sQty = "0";
                colortotal += System.Convert.ToDouble(sQty);

                sQty = row["MP"].ToString();
                if (sQty.Trim() == "") sQty = "0";
                colortotal += System.Convert.ToDouble(sQty);

                sQty = row["LP"].ToString();
                if (sQty.Trim() == "") sQty = "0";
                colortotal += System.Convert.ToDouble(sQty);

                sQty = row["XLP"].ToString();
                if (sQty.Trim() == "") sQty = "0";
                colortotal += System.Convert.ToDouble(sQty);

                row["TQTYP"] = colortotal;
                total += colortotal;
            }
            Return.Merge(tbSC);
            return Return;
        }
        public DataSet GetTask(DataRow dwInput)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            try
            {
                SResult = INTEXDBC.QueryABKOPF(dwInput, 2);
                SResult.Tables[0].TableName = "ICC_ABKOPF";
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet GetData(DataRow dwInput)
        {
            DataSet SResult = new DataSet();
            DataSet STemp = new DataSet();
            DataSet SReturn = new DataSet();
            try
            {
                string tbName = dwInput.Table.TableName;
                switch (tbName)
                {
                    case "FDECK":
                        SResult = INTEXDBC.QueryFDECK(dwInput, 3);
                        break;
                    case "WDECK":
                        SResult = INTEXDBC.QueryWDECK(dwInput, 7);
                        break;
                    case "MDECK":
                        SResult = INTEXDBC.QueryMDECK(dwInput, 3);
                        break;
                }
                SResult.Tables[0].TableName = "ICC_ABKOPF";
                SReturn.Merge(SResult);
                foreach (DataRow rx in SResult.Tables[0].Rows)
                {
                    STemp = BuildData(rx);
                    SReturn.Merge(STemp);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet BuildData(DataRow dwInput)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            try
            {
                Result = INTEXDBC.QueryABKOPF(dwInput);
                Return.Merge(Result);
                DataRow rowtemp = null;
                #region=======Order======
                foreach (DataRow row in Return.Tables["ABKOPF"].Rows)
                {
                    rowtemp = INTEXDB.ABPOS.NewRow();
                    rowtemp["ABPMANDANT"] = row["ABKMANDANT"];
                    rowtemp["ABPAUFKRS"] = row["ABKAUFKRS"];
                    rowtemp["ABPAUFLFD"] = row["ABKAUFLFD"];
                    Result = INTEXDBC.QueryABPOS(rowtemp, 2);
                    Return.Merge(Result);
                    rowtemp = INTEXDB.ICC_ABPOSFS.NewRow();                
                    rowtemp["KRS"] = row["ABKAUFKRS"];
                    rowtemp["LFD"] = row["ABKAUFLFD"];
                    Result = INTEXDBC.QueryICC_ABPOSFS(rowtemp, 3);
                    Return.Merge(Result);
                    rowtemp = INTEXDB.FDECK.NewRow();
                    rowtemp["FDMANDANT"] = row["ABKMANDANT"];
                    rowtemp["FDAUFKRS"] = row["ABKAUFKRS"];
                    rowtemp["FDAUFLFD"] = row["ABKAUFLFD"];
                    Result = INTEXDBC.QueryFDECK(rowtemp, 4);
                    Result.Tables[0].Columns.Add("RATIO", typeof(System.String));
                    Return.Merge(Result);
                    rowtemp = INTEXDB.WDECK.NewRow();
                    rowtemp["WDMANDANT"] = row["ABKMANDANT"];
                    rowtemp["WDAUFKRS"] = row["ABKAUFKRS"];
                    rowtemp["WDAUFLFD"] = row["ABKAUFLFD"];
                    Result = INTEXDBC.QueryWDECK(rowtemp, 6);
                    Result.Tables[0].Columns.Add("RATIO", typeof(System.String));
                    Return.Merge(Result);
                    rowtemp = INTEXDB.MDECK.NewRow();
                    rowtemp["MDMANDANT"] = row["ABKMANDANT"];
                    rowtemp["MDAUFKRS"] = row["ABKAUFKRS"];
                    rowtemp["MDAUFLFD"] = row["ABKAUFLFD"];
                    Result = INTEXDBC.QueryMDECK(rowtemp, 4);
                    Result.Tables[0].Columns.Add("RATIO", typeof(System.String));
                    Return.Merge(Result);
                    rowtemp = INTEXDB.GAAUF.NewRow();
                    rowtemp["GAMANDANT"] = row["ABKMANDANT"];
                    rowtemp["GAAUFKRS"] = row["ABKAUFKRS"];
                    rowtemp["GAAUFLFD"] = row["ABKAUFLFD"];
                    Result = INTEXDBC.QueryGAAUF(rowtemp, 4);
                    Result.Tables[0].Columns.Add("RATIO", typeof(System.String));
                    Return.Merge(Result);
                }
                #endregion
                #region========material==========
                var query = Return.Tables["FDECK"].AsEnumerable()
                        .Select(row => new
                        {
                            FDMANDANT = row.Field<string>("FDMANDANT"),
                            FDART = row.Field<string>("FDART"),
                            FDVAR = row.Field<string>("FDVAR")
                        }).Distinct();
                foreach (var row in query)
                {
                    rowtemp = INTEXDB.FSSTAMM.NewRow();
                    rowtemp["FSMANDANT"] = row.FDMANDANT;
                    rowtemp["FSART"] = row.FDART;
                    rowtemp["FSVAR"] = row.FDVAR;
                    Result = INTEXDBC.QueryFSSTAMM(rowtemp, 1);
                    Return.Merge(Result);
                    //FSSTAMM;FSFARBEN;FSSTUELI;FSGROE
                    rowtemp = INTEXDB.FSFARBEN.NewRow();
                    rowtemp["FSFMANDANT"] = row.FDMANDANT; ;
                    rowtemp["FSFART"] = row.FDART;
                    rowtemp["FSFVAR"] = row.FDVAR;
                    Result = INTEXDBC.QueryFSFARBEN(rowtemp, 2);
                    Return.Merge(Result);

                    rowtemp = INTEXDB.FSSTUELI.NewRow();
                    rowtemp["FSSMANDANT"] = row.FDMANDANT; ;
                    rowtemp["FSSART"] = row.FDART;
                    Result = INTEXDBC.QueryFSSTUELI(rowtemp, 2);
                    Return.Merge(Result);
                }
                var query2 = Return.Tables["WDECK"].AsEnumerable()
                    .Select(row => new
                    {
                        WDMANDANT = row.Field<string>("WDMANDANT"),
                        WDART = row.Field<string>("WDART"),
                        WDVAR = row.Field<string>("WDVAR")
                    }).Distinct();
                foreach (var row in query2)
                {
                    rowtemp = INTEXDB.WASTAMM.NewRow();
                    rowtemp["WAMANDANT"] = row.WDMANDANT;
                    rowtemp["WAART"] = row.WDART;
                    rowtemp["WAVAR"] = row.WDVAR;
                    Result = INTEXDBC.QueryWASTAMM(rowtemp, 2);
                    Return.Merge(Result);
                    //FSSTAMM;FSFARBEN;FSSTUELI;FSGROE
                    rowtemp = INTEXDB.WAFARBEN.NewRow();
                    rowtemp["WAFMANDANT"] = row.WDMANDANT;
                    rowtemp["WAFART"] = row.WDART;
                    rowtemp["WAFVAR"] = row.WDVAR;
                    Result = INTEXDBC.QueryWAFARBEN(rowtemp, 2);
                    Return.Merge(Result);
                }
                var query3 = Return.Tables["MDECK"].AsEnumerable()
                    .Select(row => new
                    {
                        MDMANDANT = row.Field<string>("MDMANDANT"),
                        MDMAT = row.Field<string>("MDMAT"),
                        MDVAR = row.Field<string>("MDVAR")
                    }).Distinct();
                foreach (var row in query3)
                {
                    rowtemp = INTEXDB.MSSTAMM.NewRow();
                    rowtemp["MSMANDANT"] = row.MDMANDANT;
                    rowtemp["MSMAT"] = Left(row.MDMAT, 14);
                    rowtemp["MSVAR"] = row.MDVAR;
                    Result = INTEXDBC.QueryMSSTAMM(rowtemp, 1);
                    Return.Merge(Result);
                }
                var query4 = Return.Tables["GAAUF"].AsEnumerable()
                    .Select(row => new
                    {
                        GAMANDANT = row.Field<string>("GAMANDANT"),
                        GAART = row.Field<string>("GAART"),
                        GAVAR = row.Field<string>("GAVAR")
                    }).Distinct();
                foreach (var row in query4)
                {
                    rowtemp = INTEXDB.GSDAT.NewRow();
                    rowtemp["GSMANDANT"] = row.GAMANDANT;
                    rowtemp["GSMAT"] = row.GAART;
                    rowtemp["GSVAR"] = row.GAVAR;
                    Result = INTEXDBC.QueryGSDAT(rowtemp, 1);
                    Return.Merge(Result);
                }
                #endregion
                #region========external producer==========
                foreach (DataRow row in Return.Tables["ABKOPF"].Rows)
                {
                    rowtemp = INTEXDB.ABFOLGE.NewRow();
                    rowtemp["ABFMANDANT"] = row["ABKMANDANT"];
                    rowtemp["ABFAUFKRS"] = row["ABKAUFKRS"];
                    rowtemp["ABFAUFLFD"] = row["ABKAUFLFD"]; 
                    Result = INTEXDBC.QueryABFOLGE(rowtemp, 4);
                    Return.Merge(Result);
                    string processed = "";
                    foreach (DataRow rowf in Result.Tables[0].Rows)
                    {
                        string ABFLIEFNR = rowf["ABFLIEFNR"].ToString().Trim();

                        if (ABFLIEFNR == "") continue;

                        if (processed.Contains(ABFLIEFNR + ";")) continue;

                        processed += ABFLIEFNR + ";";

                        rowtemp = INTEXDB.LIEFDAT.NewRow();
                        rowtemp["LSMANDANT"] = rowf["ABFMANDANT"];
                        rowtemp["LSLIEFNR"] = rowf["ABFLIEFNR"];
                        Result = INTEXDBC.QueryLIEFDAT(rowtemp, 2);
                        Return.Merge(Result);
                    }
                    string order = "";
                    string remark = "";
                   /*   order = row["ABKAUFKRS"].ToString() + row["ABKAUFLFD"].ToString() + "   ";
                    remark += GetCommentsFromTXTIND("AB3", order).Trim();
                  order = row["ABPAUFKRS"].ToString() + row["ABPAUFLFD"].ToString() + row["ABPAUFPOS"].ToString();
                    remark += GetCommentsFromTXTIND("ABP", order).Trim();
                    row["ABPFILLER1"] = Left(remark.PadRight(102, ' '), 102);*/
                #endregion
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }

        public DataSet ResetData(SETINTEXDB src)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();

            try
            {
                SResult = CreateMatrix(src.ABPOS);
                SReturn.Merge(SResult);
                SReturn.Merge(ResetGAAUF(src));
                SReturn.Merge(ResetMDECK(src));
                SReturn.Merge(ResetWDECK(src));
                SReturn.Merge(ResetFDECK(src));
                SReturn.Merge(ResetABFOLGE(src));
                SReturn.Merge(ResetABPR(src));
                SReturn.Merge(ResetICC_ABPOSFS(src));                
                SReturn.Merge(ResetABAUFM(src));
                SReturn.Merge(src.ABPOS);
                SReturn.Merge(src.WDECK);
                SReturn.Merge(src.MDECK);
                SReturn.Merge(src.FDECK);
                SReturn.Merge(src.GAAUF);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet ResetABAUFM(SETINTEXDB src)
        {
            DataSet Result = new DataSet();
            DataSet SReturn = new DataSet();
            try
            {
                /*ABASGESLG ABARSTSLG ABPSOLLMENGE7	10 ABAAENDBEN ABAAENDDAT*/
                foreach (DataRow row in src.ABPOS.Rows)
                {
                    string client = row["ABPMANDANT"].ToString();
                    string krs = row["ABPAUFKRS"].ToString();
                    string lfd = row["ABPAUFLFD"].ToString();
                    string pos = row["ABPAUFPOS"].ToString();
                    string user = row["ABPAENDBEN"].ToString();
                    string date = row["ABPAENDDAT"].ToString();
                    decimal qty = System.Convert.ToDecimal(row["ABPSOLLMENGE7"].ToString());
                    var query = from rx in src.ABAUFM.AsEnumerable()
                                where rx["ABAAUFKFRS"].ToString() == krs
                                && rx["ABAAUFKLFD"].ToString() == lfd
                                && rx["ABAAUFKPOS"].ToString() == pos
                                select rx;
                    foreach (DataRow rx in query)
                    {
                        rx["ABASGESLG"] = System.Math.Round(qty * 10, 0);
                        rx["ABARSTSLG"] = System.Math.Round(qty * 10, 0);
                        rx["ABAAENDBEN"] = user;
                        rx["ABAAENDDAT"] = date;
                    }
                }
                SReturn.Merge(src.ABAUFM);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet ResetABPR(SETINTEXDB src)
        {
            DataSet Result = new DataSet();
            DataSet SReturn = new DataSet();
            try
            {
                /*ARAENDBEN ARAENDDAT*/
                foreach (DataRow row in src.ABPOS.Rows)
                {
                    string client = row["ABPMANDANT"].ToString();
                    string krs = row["ABPAUFKRS"].ToString();
                    string lfd = row["ABPAUFLFD"].ToString();
                    string pos = row["ABPAUFPOS"].ToString();
                    string user = row["ABPAENDBEN"].ToString();
                    string date = row["ABPAENDDAT"].ToString();
                    var query = from rx in src.ABPR.AsEnumerable()
                                where rx["ABAAUFKFRS"].ToString() == krs
                                && rx["ABAAUFKLFD"].ToString() == lfd
                                && rx["ABAAUFKPOS"].ToString() == pos
                                select rx;
                    foreach (DataRow rx in query)
                    {
                        rx["ARAENDBEN"] = user;
                        rx["ARAENDDAT"] = date;
                    }
                }
                SReturn.Merge(src.ABPR);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet ResetICC_ABPOSFS(SETINTEXDB src)
        {
            DataSet Result = new DataSet();
            DataSet SReturn = new DataSet();
            try
            {
                /*ARAENDBEN ARAENDDAT*/
                foreach (DataRow row in src.ABPOS.Rows)
                {
                    string krs = row["ABPAUFKRS"].ToString();
                    string lfd = row["ABPAUFLFD"].ToString();
                    string farbe = row["ABPFARBE"].ToString();
                    string size = row["ABPGROE"].ToString().Trim();
                    string qty = row["ABPSOLLMENGE7"].ToString().Trim();
                    var query = from rx in src.ICC_ABPOSFS.AsEnumerable()
                                where rx["KRS"].ToString() == krs
                                && rx["LFD"].ToString() == lfd
                                && rx["FARBE"].ToString() == farbe
                                select rx;
                    foreach (DataRow rx in query)
                    {
                        rx[size] = qty;
                    }
                }
                SReturn.Merge(src.ABPR);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet ResetABFOLGE(SETINTEXDB src)
        {
            DataSet Result = new DataSet();
            DataSet SReturn = new DataSet();
            try
            {
                /*ABFSOLLSTK 0.01 ABFSOLLMG 10*/
                foreach (DataRow row in src.ABPOS.Rows)
                {
                    string client = row["ABPMANDANT"].ToString();
                    string krs = row["ABPAUFKRS"].ToString();
                    string lfd = row["ABPAUFLFD"].ToString();
                    string pos = row["ABPAUFPOS"].ToString();
                    string user = row["ABPAENDBEN"].ToString();
                    string date = row["ABPAENDDAT"].ToString();
                    decimal qty = System.Convert.ToDecimal(row["ABPSOLLMENGE7"].ToString());
                    var query = from rx in src.ABFOLGE.AsEnumerable()
                                where rx["ABFAUFKRS"].ToString() == krs
                                && rx["ABFAUFLFD"].ToString() == lfd
                                && rx["ABFAUFPOS"].ToString() == pos
                                select rx;
                    foreach (DataRow rx in query)
                    {
                        rx["ABFSOLLSTK"] = System.Math.Round(qty * System.Convert.ToDecimal(0.01), 0);
                        rx["ABFSOLLMG"] = System.Math.Round(qty * 10, 0);
                        rx["ABFAENDBEN"] = user;
                        rx["ABFAENDDAT"] = date;
                    }
                }
                SReturn.Merge(src.ABAUFM);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet ResetGAAUF(SETINTEXDB src)
        {
            DataSet Result = new DataSet();
            DataSet SReturn = new DataSet();
            double colortotal = 0;
            double total = 0;
            int yc = 0;
            try
            {
                DataRow dwABPOS = null;
                #region=======tbYarn=======
                DataTable tbYarn = INTEXDB.ICC_GAAUFMAT.Clone();
                tbYarn.TableName = "ICC_GAAUFMAT";
                foreach (DataRow row in src.GAAUF.Rows)
                {
                    DataRow rt = null;
                    bool newrow = false;
                    decimal lastqty = 0;

                    dwABPOS = null;
                    var query = from DataRow rx in src.ABPOS.AsEnumerable()
                                where row["ABPAUFKRS"].ToString() == row["GAAUFKRS"].ToString()
                                && row["ABPAUFLFD"].ToString() == row["GAAUFLFD"].ToString()
                                && row["ABPAUFPOS"].ToString() == row["GAAUFPOS"].ToString()
                                select rx;
                    foreach (DataRow rx in query)
                    {
                        dwABPOS = rx;
                    }
                    if (dwABPOS == null)
                        continue;
                    decimal ratio = 0;
                    decimal qty = System.Convert.ToDecimal(dwABPOS["ABPSOLLMENGE7"].ToString());
                    if (qty == 0)
                        continue;
                    decimal mqty = System.Convert.ToDecimal(row["GAKG1"].ToString());
                    string rate = row["RATIO"].ToString();
                    if (rate.Trim() == "")
                    {
                        rate = (System.Math.Round(mqty / qty, 3)).ToString();
                        row["RATIO"] = rate;
                    }
                    ratio = System.Convert.ToDecimal(rate);
                    mqty = qty * ratio;
                    row["GAKG1"] = mqty;

                    string mart = row["GAART"].ToString();
                    string mvar = row["GAVAR"].ToString();
                    string mcolor = row["GAFARBE"].ToString();
                    string mum = "KG";
                    var qg = from DataRow rowt in tbYarn.AsEnumerable()
                             where rowt["ART"].ToString() == mart
                             && rowt["VAR"].ToString() == mvar
                             && rowt["FARBE"].ToString() == mcolor
                             select rowt;
                    foreach (DataRow rowt in qg)
                    {
                        rt = rowt;
                        if (rowt["TQTY"].ToString().Trim() == "")
                        {
                            rowt["TQTY"] = "0";
                        }
                        lastqty = System.Convert.ToDecimal(rowt["TQTY"].ToString());
                    }
                    if (rt == null)
                    {
                        newrow = true;
                        rt = tbYarn.NewRow();
                    }
                    rt["ART"] = mart;
                    rt["VAR"] = mvar;
                    rt["FARBE"] = mcolor;
                    rt["UM"] = mum;
                    rt["TQTY"] = lastqty + mqty;
                    if (newrow)
                    {
                        tbYarn.Rows.Add(rt);
                    }
                }
                #endregion
                SReturn.Merge(tbYarn);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet ResetWDECK(SETINTEXDB src)
        {
            DataSet Result = new DataSet();
            DataSet SReturn = new DataSet();
            double colortotal = 0;
            double total = 0;
            int yc = 0;
            try
            {

                DataRow dwABPOS = null;
                DataTable tbFabric = INTEXDB.ICC_WDECKMAT.Clone();
                tbFabric.TableName = "ICC_WDECKMAT";
                foreach (DataRow row in src.WDECK.Rows)
                {
                    DataRow rt = null;
                    bool newrow = false;
                    decimal lastqty = 0;
                    dwABPOS = null;
                    var query = from DataRow rx in src.ABPOS.AsEnumerable()
                                where rx["ABPAUFKRS"].ToString() == row["WDAUFKRS"].ToString()
                                && rx["ABPAUFLFD"].ToString() == row["WDAUFLFD"].ToString()
                                && rx["ABPAUFPOS"].ToString() == row["WDAUFPOS"].ToString()
                                select rx;
                    foreach (DataRow rx in query)
                    {
                        dwABPOS = rx;
                    }
                    if (dwABPOS == null)
                        continue;
                    decimal ratio = 0;
                    decimal qty = System.Convert.ToDecimal(dwABPOS["ABPSOLLMENGE7"].ToString());
                    if (qty == 0)
                        continue;
                    decimal mqty = System.Convert.ToDecimal(row["WDBEDARF"].ToString());
                    string rate = row["RATIO"].ToString();
                    if (rate.Trim() == "")
                    {
                        rate = (System.Math.Round(mqty / qty, 3)).ToString();
                        row["RATIO"] = rate;
                    }
                    ratio = System.Convert.ToDecimal(rate);
                    mqty = qty * ratio;
                    row["WDBEDARF"] = mqty;

                    string mart = row["WDART"].ToString();
                    string mvar = row["WDVAR"].ToString();
                    string mcolor = row["WDFARBE"].ToString();
                    string mum = "MTR";
                    var qg = from DataRow rowt in tbFabric.AsEnumerable()
                             where rowt["ART"].ToString() == mart
                             && rowt["VAR"].ToString() == mvar
                             && rowt["FARBE"].ToString() == mcolor
                             select rowt;

                    foreach (DataRow rowt in qg)
                    {
                        rt = rowt;
                        if (rowt["TQTY"].ToString().Trim() == "")
                        {
                            rowt["TQTY"] = "0";
                        }
                        lastqty = System.Convert.ToDecimal(rowt["TQTY"].ToString());
                    }
                    if (rt == null)
                    {
                        newrow = true;
                        rt = tbFabric.NewRow();
                    }
                    rt["ART"] = mart;
                    rt["VAR"] = mvar;
                    rt["FARBE"] = mcolor;
                    rt["UM"] = mum;
                    rt["TQTY"] = lastqty + mqty;
                    if (newrow)
                    {
                        tbFabric.Rows.Add(rt);
                    }
                }
                SReturn.Merge(tbFabric);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet ResetFDECK(SETINTEXDB src)
        {
            DataSet Result = new DataSet();
            DataSet SReturn = new DataSet();
            double colortotal = 0;
            double total = 0;
            int yc = 0;
            try
            {

                DataRow dwABPOS = null;

                DataTable tbFMat = INTEXDB.ICC_FDECKMAT.Clone();
                tbFMat.TableName = "ICC_FDECKMAT";
                foreach (DataRow row in src.FDECK.Rows)
                {
                    if (row["FDTYP"].ToString() == "Z")
                        continue;
                    DataRow rt = null;
                    bool newrow = false;
                    decimal lastqty = 0;
                    dwABPOS = null;
                    var query = from DataRow rx in src.ABPOS.AsEnumerable()
                                where rx["ABPAUFKRS"].ToString() == row["FDAUFKRS"].ToString()
                                && rx["ABPAUFLFD"].ToString() == row["FDAUFLFD"].ToString()
                                && rx["ABPAUFPOS"].ToString() == row["FDAUFPOS"].ToString()
                                select rx;
                    foreach (DataRow rx in query)
                    {
                        dwABPOS = rx;
                    }
                    if (dwABPOS == null)
                        continue;
                    decimal ratio = 0;
                    decimal qty = System.Convert.ToDecimal(dwABPOS["ABPSOLLMENGE7"].ToString());
                    if (qty == 0)
                        continue;
                    decimal mqty = System.Convert.ToDecimal(row["FDMENGE"].ToString());
                    string rate = row["RATIO"].ToString();
                    if (rate.Trim() == "")
                    {
                        rate = (System.Math.Round(mqty / qty, 3)).ToString();
                        row["RATIO"] = rate;
                    }
                    ratio = System.Convert.ToDecimal(rate);
                    mqty = qty * ratio;
                    row["FDMENGE"] = mqty;

                    string mart = row["FDART"].ToString();
                    string mvar = row["FDVAR"].ToString();
                    string mcolor = row["FDFARBE"].ToString();
                    string msize = row["FDGROE"].ToString();
                    string mum = "PCS";
                    if (!tbFMat.Columns.Contains(msize))
                    {
                        tbFMat.Columns.Add(msize, typeof(System.Decimal));
                    }
                    var qg = from DataRow rowt in tbFMat.AsEnumerable()
                             where rowt["ART"].ToString() == mart
                             && rowt["VAR"].ToString() == mvar
                             && rowt["FARBE"].ToString() == mcolor
                             && rowt["SIZE"].ToString() == msize
                             select rowt;
                    foreach (DataRow rowt in qg)
                    {
                        rt = rowt;
                        if (rowt["TQTY"].ToString().Trim() == "")
                        {
                            rowt["TQTY"] = "0";
                        }
                        lastqty = System.Convert.ToDecimal(rowt["TQTY"].ToString());
                    }
                    if (rt == null)
                    {
                        newrow = true;
                        rt = tbFMat.NewRow();
                    }
                    rt["ART"] = mart;
                    rt["VAR"] = mvar;
                    rt["FARBE"] = mcolor;
                    rt["SIZE"] = msize;
                    rt["UM"] = mum;
                    rt["TQTY"] = lastqty + mqty;
                    if (newrow)
                    {
                        tbFMat.Rows.Add(rt);
                    }
                }
                SReturn.Merge(tbFMat);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet ResetMDECK(SETINTEXDB src)
        {
            DataSet Result = new DataSet();
            DataSet SReturn = new DataSet();
            double colortotal = 0;
            double total = 0;
            int yc = 0;
            try
            {
                DataRow dwABPOS = null;
                DataTable tbMat = INTEXDB.ICC_MDECKMAT.Clone();
                tbMat.TableName = "ICC_MDECKMAT";
                foreach (DataRow row in src.MDECK.Rows)
                {
                    DataRow rt = null;
                    bool newrow = false;
                    decimal lastqty = 0;
                    dwABPOS = null;
                    var query = from DataRow rx in src.ABPOS.AsEnumerable()
                                where rx["ABPAUFKRS"].ToString() == row["MDAUFKRS"].ToString()
                                && rx["ABPAUFLFD"].ToString() == row["MDAUFLFD"].ToString()
                                && rx["ABPAUFPOS"].ToString() == row["MDAUFPOS"].ToString()
                                select rx;
                    foreach (DataRow rx in query)
                    {
                        dwABPOS = rx;
                    }
                    if (dwABPOS == null)
                        continue;
                    decimal ratio = 0;
                    decimal qty = System.Convert.ToDecimal(dwABPOS["ABPSOLLMENGE7"].ToString());
                    if (qty == 0)
                        continue;
                    decimal mqty = System.Convert.ToDecimal(row["MDMENGE"].ToString());
                    string rate = row["RATIO"].ToString();
                    if (rate.Trim() == "")
                    {
                        rate = (System.Math.Round(mqty / qty, 3)).ToString();
                        row["RATIO"] = rate;
                    }
                    ratio = System.Convert.ToDecimal(ratio);
                    mqty = qty * ratio;
                    row["MDMENGE"] = mqty;

                    string mart = row["MDMAT"].ToString();
                    string mvar = row["MDVAR"].ToString();
                    string mum = "PCS";
                    var qg = from DataRow rowt in tbMat.AsEnumerable()
                             where rowt["ART"].ToString() == mart
                             && rowt["VAR"].ToString() == mvar
                             select rowt;

                    foreach (DataRow rowt in qg)
                    {
                        rt = rowt;
                        if (rowt["TQTY"].ToString().Trim() == "")
                        {
                            rowt["TQTY"] = "0";
                        }
                        lastqty = System.Convert.ToDecimal(rowt["TQTY"].ToString());
                    }
                    if (rt == null)
                    {
                        newrow = true;
                        rt = tbMat.NewRow();
                    }
                    rt["ART"] = mart;
                    rt["VAR"] = mvar;
                    rt["UM"] = mum;
                    rt["TQTY"] = lastqty + mqty;
                    if (newrow)
                    {
                        tbMat.Rows.Add(rt);
                    }
                }
                SReturn.Merge(tbMat);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public void UpdateIntex(SETINTEXDB src)
        {
            try
            {
                foreach (DataRow row in src.ABPOS.Rows)
                {
                    string client = row["ABPMANDANT"].ToString();
                    string krs = row["ABPAUFKRS"].ToString();
                    string lfd = row["ABPAUFLFD"].ToString();
                    string pos = row["ABPAUFPOS"].ToString();
                    decimal qty = System.Convert.ToDecimal(row["ABPSOLLMENGE7"].ToString());
                    INTEXDBC.DeleteICC_ABAUFM(client, krs, lfd, pos);
                    if (qty == 0)
                    {
                        INTEXDBC.DeleteABFOLGE(client, krs, lfd, pos);
                        INTEXDBC.DeleteGAAUF(client, krs, lfd, pos);
                        INTEXDBC.DeleteMDECK(client, krs, lfd, pos);
                        INTEXDBC.DeleteWDECK(client, krs, lfd, pos);
                        INTEXDBC.DeleteFDECK(client, krs, lfd, pos);
                        INTEXDBC.DeleteABPOS(client, krs, lfd, pos);
                        INTEXDBC.DeleteABPR(client, krs, lfd, pos);
                        continue;
                    }
                    INTEXDBC.UpdateABPOS(row);
                }
                foreach (DataRow row in src.WDECK.Rows)
                {
                    INTEXDBC.UpdateWDECK(row);
                }
                foreach (DataRow row in src.FDECK.Rows)
                {
                    INTEXDBC.UpdateFDECK(row);
                }
                foreach (DataRow row in src.MDECK.Rows)
                {
                    INTEXDBC.UpdateMDECK(row);
                }
                foreach (DataRow row in src.GAAUF.Rows)
                {
                    INTEXDBC.UpdateGAAUF(row);
                }
                foreach (DataRow row in src.ABPR.Rows)
                {
                    INTEXDBC.UpdateABPR(row);
                }
                foreach (DataRow row in src.ABAUFM.Rows)
                {
                    INTEXDBC.UpdateABAUFM(row);
                }
                foreach (DataRow row in src.ABFOLGE.Rows)
                {
                    INTEXDBC.UpdateABFOLGE(row);
                }
                foreach (DataRow row in src.ICC_ABPOSFS.Rows)
                {
                    INTEXDBC.UpdateICC_ABPOSFS(row,1);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
